<?php

namespace app\admin\controller;


use app\admin\model\SteelGrade;
use cmf\controller\AdminBaseController;
use think\facade\Db;

/**
 * Class SteelgradeController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   =>'钢种信息维护',
 *     'action' =>'index',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>''
 * )
 */
class SteelgradeController extends AdminBaseController
{

    public function index()
    {
        //搜索部分
//        $key_status = input('get.key_status/d');

        $lists = SteelGrade::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
        $user_arr = Db::name('user')
            ->column('user_nickname', 'id');

        $this->assign('user_arr', $user_arr);
        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);
        $companytypea_arr = array(1 => '自主', 2 => '来料',3=>'委外');
        $this->assign('companytypea_arr', $companytypea_arr);
        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(), 'domain' => request()->domain() . '/upload/']);
    }

    /**
     * @adminMenu(
     *     'name'   => '新增',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '新增',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function add()
    {
        $companytypea_arr = array(0 => array('id' => 1, 'name' => '自主'), 1 => array('id' => 2, 'name' => '来料'), 2 => array('id' => 3, 'name' => '委外'));
        $this->assign('companytypea_arr', $companytypea_arr);
        return $this->fetch('', ['domain' => request()->domain() . '/upload/']);
    }

    /**
     * @adminMenu(
     *     'name'   => '新增提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '新增提交',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function addPost()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params) {
            $this->error('非法参数');
        }
        $params['update_time'] = time();
        $params['add_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
//        SteelGrade::create($params);
        $id = Db::name('steel_grade')->insertGetId($params);
        $params['id'] = $id;
        cmf_install_log($params, 'steel_grade_log', 'add');
        $this->success('操作成功', 'index');
    }

    /**
     * @adminMenu(
     *     'name'   => '修改',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function edit()
    {
        $companytypea_arr = array(0 => array('id' => 1, 'name' => '自主'), 1 => array('id' => 2, 'name' => '来料'), 2 => array('id' => 3, 'name' => '委外'));
        $this->assign('companytypea_arr', $companytypea_arr);
        $id = input('get.id/d');

        if (!is_numeric($id)) {
            $this->error('非法参数');
        }

        $res = SteelGrade::where('id', $id)->find();


        return $this->fetch('', ['vo' => $res, 'domain' => request()->domain() . '/upload/']);
    }

    /**
     * @adminMenu(
     *     'name'   => '修改提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改提交',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function editPost()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params || empty($params['id'])) {
            $this->error('非法参数');
        }
        $params['update_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
        SteelGrade::update($params);
        cmf_install_log($params, 'steel_grade_log', 'update');
        $this->success('操作成功', 'index');
    }

    /**
     * @adminMenu(
     *     'name'   => '删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function del()
    {
        $id = input('get.id/d');

        if (!(is_numeric($id) && $id > 0)) {
            $this->error('非法参数');
        }

        SteelGrade::where('id', $id)->update(array('delete_time' => time(), 'admin_id' => cmf_get_current_admin_id()));
        $params = Db::name('steel_grade')->where('id', $id)->find();
        cmf_install_log($params, 'steel_grade_log', 'del');
        $this->success('删除成功');
    }

    public function field()
    {
        $id = input('get.id/d');
        $field = input('get.field');

        if (!(is_numeric($id) && $id > 0 && $field)) {
            $this->error('非法参数');
        }

        $info = SteelGrade::field($field)->where('id', $id)->find();

        $this->success('ok', '', $info[$field]);
    }

    public function selctid()
    {
        $id = input('get.id/d');
        $info = Db::name('g_c')->where('grade_id', $id)->where('delete_time', 0)->column('steel_grade_id');
        $infod = Db::name('steel_grade')->where('id', 'in', $info)->field('id,name')->where('delete_time', 0)->select();
        $this->success('ok', '', $infod);
    }

    public function tcpdf()
    {
//        getPageSizeFromFormat();
        $pdf = new \TCPDF('C', 'mm', array(110,180));

        //新建一个PDF文档
        //L 横排   P竖排
        $orientation = 'P';
        $unit = 'mm';
        $format = 'A4';
        $unicode = true;
        $encoding = 'UTF-8';
        $diskcache = false;
        $pdfa = false;
//        $pdf = new \TCPDF('C', 'mm', 'A4', true, 'UTF-8',false, array(120,110));

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('收据');
        $pdf->SetTitle('收据');
        $pdf->SetSubject('TCPDF Tutorial');
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

        // set default header data
        //$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 048', PDF_HEADER_STRING);

        // set header and footer fonts
        //$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        //$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        // set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        $pdf->SetFont('stsongstdlight','', 10);
//        $pdf->SetFont('droidsansfallback', '', 10);
//        $pdf->SetFont(‘droidsansfallback’, ”, 12);

        // add a page
        $pdf->AddPage();
        $pdf->Write(0, '', '', 0, 'L', true, 0, false, false, 0);

//        $pdf->setCellHeightRatio(1.3);
//        $pdf->SetLineWidth(2);

        $tbl = <<<EOD
<table border="1" cellspacing="0"   style="text-align: center; ">

    <tbody>
    <tr>
        <td width="30%" colspan="3" style="vertical-align: middle; vertical-align: middle; ">
          <img src="/upload/admin/20221224/f80dd1defbf8e2d17dd76ce22758a5e1.png" width="170" height="120">
       
        </td>
        <td width="70%" colspan="7" align="center">
         <div > 山东华星新材料科技有限公司</div>
          
          <div style="font-size:0.8em;">Shandong Huaxing New Material Tech Co., Ltd</div>
        </td>
    </tr>
    <tr>
        <td width="60%" align="center" colspan="7">
          <div>钢卷号    Coil No</div>
            <div >Y2000200002</div>
        </td>
        <td width="40%" align="center" colspan="3">
            <div>品名  Product</div>
            <div >Y2000200002</div>
        </td>
    </tr>
    <tr>
        <td width="60%" align="center" colspan="7">
            <div >捆包号 Bundle Package No</div>
            <div >Y2000200002</div>
        </td>
        <td width="40%" align="center" colspan="3">
            <div >牌号  Grade</div>
            <div >Y2000200002</div>
        </td>
    </tr>
    <tr>
        <td width="45%" align="center" colspan="7">
            <div >日期 Date</div>
            <div >Y2000200002</div>
        </td>
        <td width="55%" align="center" colspan="3">
            <div >厚宽长 D*W*H</div>
            <div>Y2000200002</div>
        </td>
    </tr>
    <tr>
        <td width="45%" align="center" colspan="7">
            <div >理重  T,W,(kg)</div>
            <div >Y2000200002</div>
        </td>
        <td width="55%" align="center" colspan="4">
            <div >产品状态 Product Status</div>
            <div >Y2000200002</div>
        </td>
    </tr>
    <tr>
      <td width="45%" align="center" colspan="3">
            <div>张数  Sheets</div>
            <div >Y2000200002</div>
        </td>
        <td width="55%" align="center" colspan="7"  >
            <div >客户名称 Customer Name</div>
            <div >Y2000200002</div>
        </td>
    </tr>
    <tr>
        <td  align="left" colspan="10">
           <img src="/upload/admin/20221224/87d3657ac13891b1a3a9192742aa8a1b.jpg" width="66" height="66">
        </td>

    </tr>
  
    </tbody>
</table>







EOD;

        $pdf->writeHTML($tbl, true, false, false, false, '');

        // -----------------------------------------------------------------------------

        //Close and output PDF document
        $pdf->Output('收据单_' . date('YmdHis') . '.pdf', 'I');
        exit();
    }

    public function tcpdfnew()
    {
//        $pdf = new \Tcpdf('C', 'mm', array(241, 93));

        $pdf = new \TCPDF('C', 'mm', array(110, 120));

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('收据');
        $pdf->SetTitle('收据');
        $pdf->SetSubject('TCPDF Tutorial');
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

        // set default header data
        //$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 048', PDF_HEADER_STRING);

        // set header and footer fonts
        //$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        //$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        // set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        //$pdf->SetFont('stsongstdlight','', 14);
//        $pdf->SetFont('droidsansfallback', '', 10);


        // add a page
        $pdf->AddPage();
        $pdf->Write(0, '', '', 0, 'L', true, 0, false, false, 0);

        $pdf->setCellHeightRatio(1.3);
        $pdf->SetLineWidth(2);

        $tbl = <<<EOD
      
      <div >
             <table cellpadding="0" cellspacing="0" style="text-align: center; width: 90%">
                <tr>
                    <th></th>
                    <th style="font-size: 25px;font-weight: bold">山东华星新材料科技有限公司(收据单)</th>
                     <td style="font-weight: bold;text-align: right;">收据单号:10000000</td>  
                </tr>
             </table>
             <table cellpadding="0" cellspacing="0" style="text-align: center; width: 90%; " >
                <tr>
                    <td style="font-weight: bold">姓名:张三</td>
                    <td style="font-weight: bold">小区名称:华星</td>
                      <td style="font-weight: bold">付款方式:微信</td>
                       <td style="font-weight: bold">付款时间: 2022-10-24 17:01</td>
                   
                </tr>

             </table>
               
             <table cellpadding="2" cellspacing="0" border="1" summary="出库单" style="align: center; width: 90%; ">
                    <tr>
                1
                    </tr>
                 1
                                      

                </table>
                
           <table cellpadding="0" cellspacing="0" style=" width: 90%" >
                <tr>
                    <td style="font-weight: bold">实收金额:1</td>
                     <td style="font-weight: bold; width: 50%">实收金额人民币(大写):1</td>
                      <td style="font-weight: bold;"></td>
                </tr>
                   <tr>
                   
                      <td style="font-weight: bold">收款人:1</td>
                     <td style="font-weight: bold">制单人:1</td>
  <td style="font-weight: bold">1</td>
                </tr>
                <b style="margin-left: 10%">备注: 本收据不作为报销凭证,盖章有效，妥善保存，遗失不补。（白-存根 红-客户 黄-记账）</b>
               
             </table>
</div>
  </body>
EOD;

        $pdf->writeHTML($tbl, true, false, false, false, '');

        // -----------------------------------------------------------------------------

        //Close and output PDF document
        $pdf->Output('收据单_' . date('YmdHis') . '.pdf', 'I');
        exit();
    }
    public function test(){
//        $html
    }
}